﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using UI;

namespace Room
{
    public class NetUnionFightTower : NetUnionFightBase
    {
        public Dictionary<long, InstUFTower> towers = new Dictionary<long, InstUFTower>();
      
        public NetUnionFightTower(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }

        public override void Enter()
        {
            base.Enter();
            towers.Clear();
        }

        public override void Leave()
        {
            base.Leave();
            towers.Clear();
        }


        public override RoomCharactor CreateCharactor(long uuid)
        {
            var inst = towers[uuid];
            charactorManager.CreateUnionTower(inst);
            towers[uuid] = inst;
            return GetCharactor(uuid);
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (towers.ContainsKey(uuid))
                towers.Remove(uuid);
        }

        /// <summary>
        /// 刷tower
        /// </summary>
        /// <param name="msg"></param>
        public void TowerRefresh(SCUFTower msg)
        {
            if (msg != null)
            {
                foreach (var tower in msg.tower)
                {
                    TowerRefresh(tower);
                }
            }
        }

        /// <summary>
        /// 刷新实例
        /// </summary>
        /// <param name="tower"></param>
        public void TowerRefresh(InstUFTower tower)
        {
            if (tower != null)
            {
                var uuid = tower.guid;
                towers[uuid] = tower;
                var charactor = GetCharactor(uuid,false);
                if (charactor == null)
                {
                    charactor = CreateCharactor(uuid);
                }

                var state = (int)UnionTowerState.None;
                
                UpdateState(uuid, state);
                var dict = Dict.Blo.DictUnionFightTowerBlo.GetTowerByID("" + tower.id);
                postions[uuid] = Dict.Blo.DictUnionFightTowerBlo.GetPosition(tower.id + "");
                UpdatePosition(uuid, postions[uuid]);

                //刷新守卫
                if (tower.guard != null)
                {
                    net.netGuard.GuardRefresh(tower.guard);
                    
                    var hp = 0.0;
                    foreach (var _hp in tower.guard.fightInfo.hpPos)
                    {
                        hp += _hp;
                    }

                    if (hp > 0)
                    {
                        states[uuid] = (int)UnionTowerState.Actived;
                    }
                    else
                    {
                        states[uuid] = (int)UnionTowerState.Unactived;
                    }

                    Debug.Log("tower hp:" + hp+" tower uuid:"+uuid);

                    UpdateState(uuid, states[uuid]);
                }
                else
                {
                    Debug.LogWarning("[DEBUG] tower guard inst null");
                }

            }
        }

        /// <summary>
        /// 塔攻击玩家
        /// </summary>
        /// <param name="msg"></param>
        public void TowerAttackActor(SCUFTowerAttackPlayer msg)
        {
            var uuid = msg.towerGuid;
            var targetUuid = msg.targetGuid;

            var tower = GetCharactor(uuid);
            var actor = GetCharactor(targetUuid);

            if (tower != null && actor != null)
            {
                var towerAi = tower.ai as RCAITower;
                if (towerAi != null)
                {
                    towerAi.Attack(actor, msg.releaseTime);
                }
            }
        }

        /// <summary>
        /// 塔攻击战车
        /// </summary>
        /// <param name="msg"></param>
        public void TowerAttackCariot(SCUFTowerAttackCariot msg)
        {
            Debug.Log("tower attack chariot tower uuid:" + msg.towerGuid + " target uuid:" + msg.targetGuid);
            var uuid = msg.towerGuid;
            var targetUuid = msg.targetGuid;
            var tower = GetCharactor(uuid);
            
            var chariot = GetCharactor(targetUuid);
            if(tower!= null && chariot!=null)
            {
                var towerAi = tower.ai as RCAITower;
                if(towerAi!=null)
                {
                    towerAi.Attack(chariot, msg.releaseTime);
                }
            }
        }

        /// <summary>
        /// 塔被摧毁，守卫挂了
        /// </summary>
        /// <param name="msg"></param>
        public void TowerDestroy(SCUFTowerCrash msg)
        {
            var uuid =  msg.towerGuid;
            
            states[uuid] = (int)UnionTowerState.Unactived;
            UpdateState(uuid, states[uuid]);
            
            
            //守卫的逻辑
            //net.netGuard.GuardDead(dict.UFGuardId);
        }
        
    }
}